package com.example.employ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.employ.common.convention.exception.ClientException;
import com.example.employ.common.convention.exception.ServiceException;
import com.example.employ.dao.entity.ResumeDO;
import com.example.employ.dao.mapper.ResumeMapper;
import com.example.employ.dto.request.ResumeRemoveDTO;
import com.example.employ.dto.request.ResumeSaveReqDTO;
import com.example.employ.dto.request.ResumeUpdateReqDTO;
import com.example.employ.dto.response.ResumeDetailRespDTO;
import com.example.employ.service.ResumeService;
import org.springframework.stereotype.Service;

import static com.example.employ.common.enums.ResumeErrorCodeEnums.*;
import static com.example.employ.common.enums.UserErrorCodeEnums.USER_EXIST;

/**
 * @author LYT0905
 * @date 2024/05/23/15:06
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, ResumeDO> implements ResumeService {
    /**
     * 新增简历
     * @param requestParam 请求参数
     */
    @Override
    public void saveResume(ResumeSaveReqDTO requestParam) {
        // 插入数据库
        ResumeDO resumeDO = ResumeDO.builder()
                .userId(requestParam.getUserId())
                .phone(requestParam.getPhone())
                .username(requestParam.getUsername())
                .sex(requestParam.getSex())
                .experience(requestParam.getExperience())
                .build();
        int insert = baseMapper.insert(resumeDO);
        // 没插入成功
        if (insert < 1){
            throw new ServiceException(RESUME_SAVE_ERROR);
        }
    }

    /**
     * 删除简历
     * @param userId 用户id
     */
    @Override
    public void removeResume(Long userId) {
        // 根据用户id找到并且把删除标志为 1，代表删除
        LambdaUpdateWrapper<ResumeDO> updateWrapper = Wrappers.lambdaUpdate(ResumeDO.class)
                .eq(ResumeDO::getUserId, userId)
                .set(ResumeDO::getDelFlag, 1);
        int update = baseMapper.update(null, updateWrapper);
        if (update < 1){
            throw new ServiceException(RESUME_REMOVE_ERROR);
        }
    }

    /**
     * 获取简历详情
     * @param userId 用户id
     * @return
     */
    @Override
    public ResumeDetailRespDTO detail(Long userId) {
        // 根据用户id以及没有被删除来获取
        LambdaQueryWrapper<ResumeDO> queryWrapper = Wrappers.lambdaQuery(ResumeDO.class)
                .eq(ResumeDO::getUserId, userId)
                .eq(ResumeDO::getDelFlag, 0);
        ResumeDO resumeDO = baseMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(resumeDO, ResumeDetailRespDTO.class);
    }

    /**
     * 修改简历信息
     * @param requestParam 请求参数
     */
    @Override
    public void updateResume(ResumeUpdateReqDTO requestParam) {
        LambdaUpdateWrapper<ResumeDO> updateWrapper = Wrappers.lambdaUpdate(ResumeDO.class)
                .eq(ResumeDO::getUserId, requestParam.getUserId())
                .set(ResumeDO::getPhone, requestParam.getPhone())
                .set(ResumeDO::getSex, requestParam.getSex())
                .set(ResumeDO::getUsername, requestParam.getUsername())
                .set(ResumeDO::getExperience, requestParam.getExperience());
        int update = baseMapper.update(null, updateWrapper);
        if (update < 1){
            throw new ServiceException(RESUME_UPDATE_ERROR);
        }
    }
}
